package com.example.wumeng.spinner;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class ThirdActivity extends AppCompatActivity {

    private Spinner spinner,spinnerCountry;

    private TextView textView;

    private List<CharSequence> data = null;

    private ArrayAdapter<CharSequence> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);

        spinner = findViewById(R.id.spinner);
        textView = findViewById(R.id.text);
        spinnerCountry = findViewById(R.id.spinnerCountry);

        spinnerCountry.setPrompt("请选择:");
        data = new ArrayList<>();
        data.add("中国");
        data.add("美国");
        data.add("日本");

        adapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_dropdown_item,data);
        spinnerCountry.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String[] citys = getResources().getStringArray(R.array.city_labels);
                textView.setText("你喜欢的城市是:" + citys[position]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}
